package com.ian.config;

import com.ian.entity.ConfigInfo;
import com.ian.entity.ConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * AutoConfiguration 类
 * @author Witt
 * @version 1.0.0
 * @date 2022/7/21
 */
@Configuration
@ConditionalOnClass(ConfigInfo.class)
// 根据 custom.config.enabled 属性的值来决定是否加载该配置类 ConfigAutoConfiguration 到上下文中。这2种情况下加载：“有值且为 true”，或者 “无值”
@ConditionalOnProperty(name = "custom.config.enabled", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(ConfigProperties.class)
public class ConfigAutoConfiguration {

    @Resource
    private ConfigProperties configProperties;

    @Bean
    @ConditionalOnMissingBean
    public ConfigInfo configInfo() {
        ConfigInfo configInfo = ConfigInfo.builder()
                .id(configProperties.getId())
                .ip(configProperties.getIp())
                .enabled(configProperties.isEnabled())
                .build();
        return configInfo;
    }
}
